package com.hsxc.cdz.monitor.FSM;

import com.hsxc.cdz.base.bean.Bill;
import com.hsxc.cdz.base.util.TimeFormatter;
import com.hsxc.cdz.mgmt.GlobalManager;
import com.hsxc.cdz.monitor.executor.ServerExecutorNew;
import com.hsxc.cdz.monitor.executor.ServerExecutor;
import com.hsxc.cdz.trans.bill.CacheTrade;
import com.hsxc.cdz.trans.bill.CacheTrade.TradeStatus;

public class PileMemStateCharging extends PileMemState {

	// 构造函数
	public PileMemStateCharging(CachePile pile) {
		this.status = Status.PILE_STATUS_CHARGING;
		this.pile = pile;
	}

	// event actions
	// ===========================================================================
	@Override
	int fault() {
		// handleFault();

		// 状态转移
		this.status = Status.PILE_STATUS_FAULT;
		this.pile.state = new PileMemStateFault(this.pile);

		// 更新DB
		updatePileStatus(this.pile);
		//更新缓存
		GlobalManager.pileManager.delPile(this.pile.getAddress());
		//处理进行中的交易
//		CacheTrade cacheTrade = GlobalManager.tradeManager.getTradeByPileAddress(this.pile.getAddress());
//		if(cacheTrade!=null){
//			//GlobalManager.tradeManager.u
//		}
		return 0;
	}

	@Override
	int recovery() {
		return 1;
	}

	int lock() {
		return CachePile.PILE_STATUS_LOCKED;
	}

	int unlock() {
		return 1;
	}

	@Override
	int heartBeatLost() {
		return 0;
	}

	@Override
	public int logout() {
		this.pile.state = new PileMemStateIdle(this.pile);
		updatePileStatus(this.pile);
		GlobalManager.pileManager.delPile(this.pile.getAddress());
		return 0;
	}

	@Override
	public int reboot() {
		return 1;
	}

	@Override
	public int chargeStart_jiaoliu(int chargePort, int startWay, int ctrlWay, int stopWay, double limitData,
			String startTime, String cardNum, String pileAddress, String operatorNO, String protocol, int pileType) {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public int chargeStart_zhiliu(int chargePort, int startWay, int ctrlWay, int stopWay, double limitData,
			String startTime, String cardNum, String pileAddress, String operatorNO, int chargeDataInterval,
			int batteryDataInteval, String protocol, int pileType) {
		return 1;
	}

	@Override
	public boolean confirmStart(int chargePort, String pileNo, String userNo, String protocol, int pileType) {
		return false;
	}

	@Override
	public int chargeStop(int chargePort, int control, String userNO, String protocol, int pileType) {
		return ServerExecutor.stopCharge(0, 0, 0, 0, userNO, this.pile.getAddress(), this.pile.getOperator(),
				this.pile.getProtocolVersion(), this.pile.getPileType());
	}

	@Override
	public boolean confirmStop(int chargePort, String pileNo, String userNo, String protocol, int pileType) {
		CacheTrade cacheTrade = GlobalManager.tradeManager.getTrade(pileNo, userNo);
		if (cacheTrade != null) {
			// 非刷卡交易
			// 修改交易信息
			cacheTrade.setTradeStatus(TradeStatus.TRADE_STATUS_NOPAY);
			cacheTrade.setStopTime(TimeFormatter.toTimeYear());
			GlobalManager.tradeManager.modifyTrade(cacheTrade.getTid(), cacheTrade);

		 // 更新账单状态
			Bill bill = GlobalManager.billManager.getBillByBillNO(cacheTrade.getTid());
			if (bill != null) {
				bill.setState(Bill.NO_PAY);
				//bill.setGenerateTime(TimeFormatter.toTimeYear());
				bill.setStopTime(TimeFormatter.toTimeYear());
				GlobalManager.billManager.updateBill(bill);
			}
		}
		this.pile.state = new PileMemStateIdle(this.pile);
		//将实时充电数据清空。
		this.pile.init();
		//更新数据库
		if (updatePileStatus(this.pile) == 1) {
			return true;
		}
		return false;
	}

	@Override
	int reserve(int chargePort, String cardNo, int duration, String pileAddress, String operatorNo, String protocol,
			int pileType) {
		return 1;
	}

	@Override
	int confirmReserv_jiaoliu(int chargePort, String cardNo, String pileAddress, String operator, String protocol,
			int pileType) {
		return 1;
	}

	@Override
	int confirmReserv_zhiliu(int chargePort, String cardNo, String resStartTime, String pileAddress, String operator,
			String protocol, int pileType) {
		return 1;
	}

	@Override
	int reservationCancel(int chargePort, String cardNum, String pileadress, String operatorNo, String protocol,
			int pileType) {
		return 1;
	}

	@Override
	int confirmReservCancelled(int chargePort, String cardNo, String pileAddress, String operatorNo, String protocol,
			int pileType) {
		return 1;
	}
}
